<?php
/**
 * Bootstrap
 *
 * @category    IPB_CMS
 * @package     Application
 * @copyright   Copyright (c) 2009 IPB CMS <http://ipb-cms.googlecode.com/>
 * @license     http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License
 * @author      Henrique Moody <henriquemoody@gmail.com>
 */
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{

    /**
     * Name of locale
     *
     * @var string
     */
    protected $_locale;

    /**
     * Name of locale path
     *
     * @var string|null
     */
    protected $_localePath = null;

    /**
     * Setup the logging
     *
     * @return void
     */
    protected function _initLogger()
    {
        $logger    = new Zend_Log();
        $format    = "%timestamp% %priorityName%: %message%" . PHP_EOL;
        $formatter = new Zend_Log_Formatter_Simple($format);
        if ( APPLICATION_ENV == 'production' ) {
            $writter = new Zend_Log_Writer_Stream(APPLICATION_PATH . '/logs/app.log');
            $logger->addFilter(Zend_Log::ALERT);
            $logger->addFilter(Zend_Log::CRIT);
            $logger->addFilter(Zend_Log::WARN);
            $logger->addFilter(Zend_Log::ERR);
        } else {
            $writter = new Zend_Log_Writer_Firebug();
        }
        $writter->setFormatter($formatter);
        $logger->addWriter($writter);
        Zend_Registry::set('Zend_Log', $logger);
        return $logger;
    }

    /**
     * Initialize autoload
     *
     * @return void
     */
    public function _initAutoload()
    {
        $loader = new Zend_Application_Module_Autoloader(array(
            'namespace' => '',
            'basePath'  => APPLICATION_PATH . '/modules/default')
        );
        // Logging
        Zend_Registry::get('Zend_Log')->info(__METHOD__);
    }

    /**
     * Setup the database profiling
     *
     * @return void
     */
    protected function _initDbProfiler()
    {
        if (APPLICATION_ENV != 'production') {
            $this->bootstrap('db');
            $profiler = new Zend_Db_Profiler_Firebug('All DB Queries');
            $profiler->setEnabled(true);
            $this->getPluginResource('db')->getDbAdapter()->setProfiler($profiler);
        }
        // Logging
        Zend_Registry::get('Zend_Log')->info(__METHOD__);
    }

    /**
     * View and Helpers
     *
     * @return void
     */
    protected function _initViewHelpers()
    {
        $this->bootstrap('layout');
        $layout = $this->getResource('layout');
        $view = $layout->getView();
        $view->doctype('XHTML1_STRICT');
        $view->setEncoding('UTF-8');
        $view->headMeta()->appendHttpEquiv('Content-type','text/html; charset=UTF-8');
        $view->setBasePath(APPLICATION_PATH . '/modules/default/views', 'View');
        $view->addHelperPath(
            'App/View/Helper',
            'App_View_Helper'
        );
        $view->addHelperPath(
            'ZendX/JQuery/View/Helper',
            'ZendX_JQuery_View_Helper'
        );
        Zend_Paginator::setDefaultItemCountPerPage(10);
        Zend_View_Helper_PaginationControl::setDefaultViewPartial(
            'common/paginator.phtml'
        );
        // Logging
        Zend_Registry::get('Zend_Log')->info(__METHOD__);
    }

    /**
     * Initializa locale settings
     *
     * @return void
     */
    protected function _initLocale()
    {
        Zend_Locale::setDefault('en');
        $current = Zend_Locale::findLocale();
        $path = realpath( APPLICATION_PATH . '/../languages/' . $current . '/LC_MESSAGES' );
        $this->_locale     = $current;
        $this->_localePath = is_dir($path) ? $path : null;
        Zend_Registry::set('Zend_Locale', new Zend_Locale($current));
        // Logging
        Zend_Registry::get('Zend_Log')->info(__METHOD__);
        Zend_Registry::get('Zend_Log')->info('Current locale "' . $current . '"');
    }

    /**
     * Initialize internationalization
     *
     * @return void
     */
    protected function _initInternationalization()
    {
        if(null === $this->_localePath) {
            return;
        }
        $applicationAdapter = new Zend_Translate(
            'gettext',
            $this->_localePath . '/application.mo',
            $this->_locale
        );
        Zend_Registry::set('Zend_Translate', $applicationAdapter);
        // Logging
        Zend_Registry::get('Zend_Log')->info(__METHOD__);
    }

}
